package com.mall.b2c.template.directive;

import java.io.IOException;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.mall.b2c.bean.GoodsCategoryBean;
import com.mall.b2c.hessian.GoodsHessian;
import com.mall.common.directive.DirectiveUtils;
import com.mall.common.directive.exception.ParamsRequiredException;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * 内容对象标签
 */
@Repository
public class GoodsCategoryDirective extends BaseDirective {

	@Resource private GoodsHessian goodsHessian;
	/**分类ID**/
	public static final String PARAM_ID = "id";

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void execute(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		Long id = DirectiveUtils.getLong(PARAM_ID, params);
		if(id == null){
			throw new ParamsRequiredException(PARAM_ID);
		}
		GoodsCategoryBean goodsCategory = jsonToObj(goodsHessian.getCoodsCategory(id), GoodsCategoryBean.class);		
		renderParamToBody("category", goodsCategory, env, body);
	}

}
